<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Object-oriented programming in Visual Basic</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Visual Basic, learn Visual Basic, OOP, object oriented 
programming, programming, language">
<meta name="description" content="This part of the Visual Basic tutorial covers 
Object-oriented programming in Visual Basic.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">

<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Object-oriented programming</h1>

<p>
In this part of the Visual Basic tutorial, we will talk about object oriented 
programming in Visual Basic.
</p>

<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div>

<p>
There are three widely used programming paradigms there. Procedural 
programming, functional programming and object-oriented programming. 
Visual Basic supports both procedural and object-oriented programming.
</p>

<p>
<b>Object-oriented programming (OOP)</b> is a programming paradigm that uses objects
and their interactions to design applications and computer programs. (Wikipedia)
</p>

<p>
There are some basic programming concepts in OOP:
</p>

<ul>
<li>Abstraction</li>
<li>Polymorphism</li>
<li>Encapsulation</li>
<li>Inheritance</li>
</ul>

<p>
The <b>abstraction</b> is simplifying complex reality by modeling classes appropriate to the problem. 
The <b>polymorphism</b> is the process of using an operator or function in different ways for different data input. 
The <b>encapsulation</b> hides the implementation details of a class from other objects.
The <b>inheritance</b> is a way to form new classes  using classes that have already been defined.
</p>

<h2>Objects</h2>

<p>
Objects are basic building blocks of a Visual Basic OOP program. An object is a combination of data and methods.
In a OOP program, we create objects. These objects communicate together through methods. Each object 
can receive messages, send messages and process data. 
</p>

<p>
There are two steps in creating an object. First, we create a class. A <b>class</b> is a template for an object. 
It is a blueprint, which describes the state and behavior that the objects of the class all share. A class
can be used to create many objects. Objects created at runtime from a class are called <b>instances</b> of that 
particular class. 
</p>

<pre class="code">
Option Strict On


Module Example

    Class Being
    
    End Class

    Sub Main()
        
        Dim b as New Being
        Console.WriteLine(b.ToString())      

    End Sub
    
End Module
</pre>

<p>
In our first example, we create a simple object. 
</p>

<pre class="explanation">
Class Being

End Class
</pre>

<p>
This is a simple class definition. The body of the template is empty. It does not
have any data or methods. 
</p>

<pre class="explanation">
Dim b as New Being
</pre>

<p>
We create a new instance of the Being class. For this we have the <code>New</code>
keyword. The b variable is the handle to the created object. 
</p>

<pre class="explanation">
Console.WriteLine(b.ToString())
</pre>

<p>
The <code>ToString()</code> method of the object
gives some basic description of the object. 
</p>

<pre>
$ ./object.exe 
Example+Being
</pre>

<p>
We don't get much info, since the class definition was empty. 
We get the object class name and the module name, where the
instance of this object was created.
</p>

<h2>Object attributes</h2>

<p>
Object attributes is the data bundled in an instance of a class. The 
object attributes are called <b>instance variables</b> or <b>member fields</b>.
An instance variable is a variable defined in a class, for which each object
in the class has a separate copy.
</p>

<pre class="code">
Option Strict On


Module Example

    Class Person

        Public Name As String
    
    End Class

    Sub Main()
        
        Dim p1 as New Person
        p1.Name = "Jane"

        Dim p2 as New Person
        p2.Name = "Beky"

        Console.WriteLine(p1.Name)
        Console.WriteLine(p2.Name)      

    End Sub
    
End Module
</pre>

<p>
In the above Visual Basic code, we have a Person class with
one member field. 
</p>


<pre class="explanation">
Class Person
    Public Name As String
End Class
</pre>

<p>
We declare a Name member field. The <code>Public</code>
keyword specifies, that the member field will be accessible
outside the Class/End Class block.
</p>

<pre class="explanation">
Dim p1 as New Person
p1.Name = "Jane"
</pre>

<p>
We create an instance of the Person class. And set the
Name variable to "Jane". We use the dot operator to 
access the attributes of objects. 
</p>

<pre class="explanation">
Dim p2 as New Person
p2.Name = "Beky"
</pre>

<p>
We create another instance of the Person class. Here
we set the variable to "Beky".
</p>

<pre class="explanation">
Console.WriteLine(p1.Name)
Console.WriteLine(p2.Name)
</pre>

<p>
We print the contents of the variables to the console. 
</p>

<pre>
$ ./person.exe 
Jane
Beky
</pre>

<p>
We see the output of the program. Each instance of the Person class has a separate
copy of the Name member field. 
</p>

<h2>Methods</h2>

<p>
Methods are functions/procedures defined inside the body of a class. They are used to perform operations 
with the attributes of our objects. Methods are essential in <b>encapsulation</b> concept of 
the OOP paradigm. For example, we might have a Connect method in our AccessDatabase class. 
We need not to be informed, how exactly the method Connect connects to the database. 
We only know, that it is used to connect to a database. This is essential in dividing
responsibilities in programming. Especially in large applications. 
</p>

<pre class="code">
Option Strict On


Module Example

    Class Circle

        Public Radius As Integer

        Public Sub SetRadius(ByVal Radius As Integer)
            Me.Radius = Radius
        End Sub

        Public Function Area() As Double
            Return Me.Radius * Me.Radius * Math.PI
        End Function
    
    End Class

    Sub Main()
        
        Dim c As New Circle
        c.SetRadius(5)

        Console.WriteLine(c.Area())

    End Sub
    
End Module
</pre>

<p>
In the code example, we have a Circle class. We define two methods.
</p>

<pre class="explanation">
Public Radius As Integer
</pre>

<p>
We have one member field. It is the Radius of the circle. The <code>Public</code>
keyword is an access specifier. It tells that the variable is fully accessible from the outside
world. 
</p>

<pre class="explanation">
Public Sub SetRadius(ByVal Radius As Integer)
    Me.Radius = Radius
End Sub
</pre>

<p>
This is the SetRadius() method. It is a normal Visual Basic procedure. 
The <code>Me</code> variable is a special variable, which
we use to access the member fields from methods. 
</p>

<pre class="explanation">
Public Function Area() As Double
    Return Me.Radius * Me.Radius * Math.PI
End Function
</pre>

<p>
The Area() method returns the area of a circle. The <code>Math.PI</code> is 
a built-in constant. 
</p>

<pre>
$ ./circle.exe 
78.5398163397448
</pre>

<p>
Running the example. 
</p>


<h2>Access modifiers</h2>

<p>
<b>Access modifiers</b> set the visibility of methods and member fields. Visual Basic has five access modifiers.
<code>Public</code>, <code>Protected</code>, <code>Private</code>, <code>Friend</code>
and <code>ProtectedFriend</code>.
<code>Public</code> members can be accessed from anywhere. <code>Protected</code> 
members can be accessed only within the class itself and by inherited and parent classes. 
<code>Friend</code> members may be accessed from within the same assembly (exe or dll).
<code>ProtectedFriend</code> is a union of protected and friend modifiers.
</p>

<p>
Access modifiers protect data against accidental modifications. They make the programs more robust.
</p>

<pre class="code">
Option Strict On


Module Example

    Class Person

        Public Name As String
        Private Age As Byte

        Public Function GetAge() As Byte
            Return Me.Age
        End Function

        Public Sub SetAge(ByVal Age As Byte)
            Me.Age = Age
        End Sub
    
    End Class

    Sub Main()
        
        Dim p as New Person
        p.Name = "Jane"

        p.setAge(17)

        Console.WriteLine("{0} is {1} years old", _
           p.Name, p.GetAge)

    End Sub
    
End Module
</pre>

<p>
In the above program, we have two member fields. One is declared Public, 
the other Private. 
</p>

<pre class="explanation">
Public Function GetAge() As Byte
    Return Me.Age
End Function
</pre>

<p>
If a member field is Private, the only way to access it is via
methods. If we want to modify an attribute outside the class, the
method must be declared Public. This is an important aspect of 
data protection. 
</p>

<pre class="explanation">
Public Sub SetAge(ByVal Age As Byte)
    Me.Age = Age
End Sub
</pre>

<p>
The SetAge() method enables us to change the Private Age variable
from outside of the class definition.
</p>

<pre class="explanation">
Dim p as New Person
p.Name = "Jane"
</pre>

<p>
We create a new instance of the Person class. 
Because the Name attribute is Public, we can access it
directly. However, this is not recommended. 
</p>

<pre class="explanation">
p.setAge(17)
</pre>

<p>
The SetAge() method modifies the Age member field. It cannot be accessed
or modified directly, because it is declared Private. 
</p>

<pre class="explanation">
Console.WriteLine("{0} is {1} years old", _
    p.Name, p.GetAge)
</pre>

<p>
Finally, we access both members to build a string.
</p>

<pre>
$ ./modifiers.exe 
Jane is 17 years old
</pre>

<p>
Running the example. 
</p>

<hr class="btm">

<pre class="code">
Option Strict On


Module Example

    Class Base

        Public Name As String = "Base"
        Protected Id As Integer = 5323
        Private IsDefined As Boolean = True
    
    End Class

    Class Derived 
        Inherits Base
    
        Public Sub Info()
            Console.WriteLine("This is Derived Class")
            Console.WriteLine("Members inherited:")
            Console.WriteLine(Me.Name)
            Console.WriteLine(Me.Id)
            'Console.WriteLine(Me.IsDefined)
        End Sub

    End Class

    Sub Main()
        
        Dim drv As Derived = New Derived
        drv.Info()
     
    End Sub
    
End Module
</pre>

<p>
In the preceding program, we have a Derived class, which inherits from the Base class. The
Base class has three member fields. All with different access modifiers. 
The IsDefined member is not inherited. The <code>Private</code>
modifier prevents this.
</p>

<pre class="explanation">
Class Derived 
    Inherits Base
</pre>

<p>
The class Derived inherits from the Base class. 
</p>

<pre class="explanation">
Console.WriteLine(Me.Name)
Console.WriteLine(Me.Id)
'Console.WriteLine(Me.IsDefined)
</pre>

<p>
The Public and the Protected members are inherited by the Derived class.
They can be accessed. The Private member is not inherited. The line
accessing the member field is commented. If we uncommented the line,
it would not compile.
</p>


<pre>
$ ./protected.exe 
This is Derived Class
Members inherited:
Base
5323
</pre>

<p>
Running the program, we receive this output. The Public and Protected members are inherited, the 
Private member is not. 
</p>



<h2>Method overloading</h2>

<p>
<b>Method overloading</b> allows the creation of several methods with the same name 
which differ from each other in the type of the input.
</p>

<p>
What is method overloading good for? The Qt4 library gives a nice example for the usage. 
The QPainter class has three methods to draw a rectangle. Their name is drawRect() and their
parameters differ. One takes a reference to a floating point rectangle object, another takes
a reference to an integer rectangle object and the last one takes four parameters, x, y, width, height.
If the C++ language, which is the language in which Qt is developed, didn't have method overloading, the
creators of the library would have to name the methods like drawRectRectF(), drawRectRect(), drawRectXYWH().
The solution with method overloading is more elegant. 
</p>

<pre class="code">
Option Strict On


Module Example

    Class Sum

        Public Function GetSum() As Integer
            Return 0
        End Function

        Public Function GetSum(ByVal x As Integer) As Integer
            Return x
        End Function

        Public Function GetSum(ByVal x As Integer, _
            ByVal y As Integer) As Integer
            Return x + y
        End Function
    
    End Class

    Sub Main()
        
        Dim s As Sum = New Sum
        
        Console.WriteLine(s.getSum())
        Console.WriteLine(s.getSum(20))
        Console.WriteLine(s.getSum(20, 30))

    End Sub
    
End Module
</pre>

<p>
We have three methods called GetSum(). They differ in input parameters.
</p>

<pre class="explanation">
Public Function GetSum(ByVal x As Integer) As Integer
    Return x
End Function
</pre>

<p>
This one takes one parameter.
</p>

<pre class="explanation">
Console.WriteLine(s.getSum())
Console.WriteLine(s.getSum(20))
Console.WriteLine(s.getSum(20, 30))
</pre>

<p>
We call all three methods. 
</p>

<pre class="explanation">
$ ./overloading.exe 
0
20
50
</pre>

<p>
And this is what we get, when we run the 
example.
</p>


<h2>The constructor</h2>

<p>
A constructor is a special kind of a method. It is automatically called, when the object is created. 
The purpose of the constructor is to initiate the state of the object. 
The name of the constructor in Visual Basic is <code>New</code>. The constructors
are methods, so they can be overloaded too.
</p>

<pre class="code">
Option Strict On


Module Example

    Class Being
    
        Sub New()
            Console.WriteLine("Being is being created")
        End Sub
        
        Sub New(ByVal name As String)
            Console.WriteLine("Being {0} is created", name)
        End Sub
    
    End Class
    
    Sub Main()
        
        Dim b As New Being
        Dim t As New Being("Tom")
                
    End Sub
    
End Module
</pre>

<p>
We have a Being class. This class has two constructors.
The first one does not take parameters, the second one
takes one parameter. 
</p>

<pre class="explanation">
Sub New(ByVal name As String)
    Console.WriteLine("Being {0} is created", name)
End Sub
</pre>

<p>
This constructor takes one String parameter.
</p>

<pre class="explanation">
Dim b As New Being
</pre>

<p>
An instance of the Being class is created. This 
time the constructor without a parameter is called upon
object creation. 
</p>

<pre>
$ ./constructor.exe 
Being is being created
Being Tom is created
</pre>

<p>
This is the output of the program. 
</p>

<hr class="btm">

<p>
In the next example, we initiate data members of the class.
Initiation of variables is a typical job for constructors. 
</p>


<pre class="code">
Option Strict On


Module Example

    Class MyFriend
    
        Private Born As Date
        Private Name As String
        
        Sub New(ByVal Name As String, ByVal Born As Date)
            Me.Name = Name
            Me.Born = Born
        End Sub

        Public Sub GetInfo()
            Console.WriteLine("{0} was born on {1}", _
                Me.Name, Me.Born.ToShortDateString)
        End Sub
    
    End Class
    
    Sub Main()
    
        Dim name As String = "Lenka"    
        Dim born As Date = #5/3/1990#
    
        Dim fr As MyFriend = New MyFriend(name, born)
        fr.GetInfo()
        
    End Sub
    
End Module
</pre>

<p>
We have a Friend class with data members and methods. 
</p>

<pre class="explanation">
Private Born As Date
Private Name As String
</pre>

<p>
We have two variables in the class definition. The <code>Private</code> keyword is
an access modifier. It is a form of encapsulation. The <code>Private</code> keyword
is the most restrictive modifier. It allows only the object in question to access the variable. 
No descendants, no other objects. 
</p>

<pre class="explanation">
Sub New(ByVal Name As String, ByVal Born As Date)
    Me.Name = Name
    Me.Born = Born
End Sub
</pre>

<p>
In the constructor, we initiate the two data members. The <code>Me</code> variable
is a handler used to reference the object variables. 
</p>

<pre class="explanation">
Dim fr As MyFriend = New MyFriend(name, born)
fr.GetInfo()
</pre>

<p>
We create a Friend object with two arguments. Then we call the <code>GetInfo()</code> method
of the object. 
</p>

<pre>
./constructor2.exe 
Lenka was born on 5/3/1990
</pre>


<h2>Class constants</h2>

<p>
Visual Basic enables to create class constants. These constants do not belong 
to a concrete object. They belong to the class. By convention, constants are 
written in uppercase letters. 
</p>

<pre class="code">
Option Strict On


Module Example

    Class Math

        Public Const PI As Double = 3.14159265359
    
    End Class

    Sub Main()
         Console.WriteLine(Math.PI)    
    End Sub
    
End Module
</pre>

<p>
We have a Math class with a PI constant. 
</p>

<pre class="explanation">
Public Const PI As Double = 3.14159265359
</pre>

<p>
The <code>Const</code> keyword is used to define a constant.
</p>

<pre>
$ ./classconstant.exe 
3.14159265359
</pre>

<p>
Running the example.
</p>


<h2>The ToString() method</h2>

<p>
Each object has a <code>ToString()</code> method. It returns a human-readable
representation of the object. The default implementation returns the fully qualified name 
of the type of the Object.
Note that when we call the <code>Console.WriteLine()</code> method with 
an object as a parameter, the <code>ToString()</code> is being called.
</p>

<pre class="code">
Option Strict On


Module Example

    Class Being
    
        Public Overrides Function ToString As String    
            Return "This is Being Class"
        End Function
    
    End Class

    Sub Main()
        
        Dim b as New Being
        Dim o As New Object

        Console.WriteLine(o.ToString())
        Console.WriteLine(b.ToString())     
        Console.WriteLine(b) 

    End Sub
    
End Module
</pre>

<p>
We have a Being class in which we override the default implementation
of the <code>ToString()</code> method. 
</p>

<pre class="explanation">
Public Overrides Function ToString As String    
    Return "This is Being Class"
End Function
</pre>

<p>
Each class created inherits from the base <code>Object</code>. 
The <code>ToString()</code> method belongs to this Object class.
We use the <code>Overrides</code> keyword to inform, that
we are overriding a method.
</p>

<pre class="explanation">
Dim b as New Being
Dim o As New Object
</pre>

<p>
We create two objects. One custom defined and one built-in. 
</p>

<pre class="explanation">
Console.WriteLine(o.ToString())
Console.WriteLine(b.ToString())  
</pre>

<p>
We call the <code>ToString()</code> method on these two objects.
</p>

<pre class="explanation">
Console.WriteLine(b) 
</pre>

<p>
As we have specified earlier, calling the <code>Console.WriteLine()</code>
on the object will call its <code>ToString()</code> method.
</p>

<pre>
$ ./override.exe 
System.Object
This is Being Class
This is Being Class
</pre>

<p>
This is what we get, when we run the script. 
</p>


<h2>Inheritance</h2>

<p>
The inheritance is a way to form new classes using classes that have already been defined. 
The newly formed classes are called <b>derived</b> classes, the classes that we derive from
are called <b>base</b> classes. Important benefits of inheritance are code reuse and reduction 
of complexity of a program. The derived classes (descendants) override or extend the 
functionality of base classes (ancestors). 
</p>

<pre class="code">
Option Strict On


Module Example

    Class Being
        Sub New()
            Console.WriteLine("Being is created")
        End Sub
    End Class
    
    Class Human 
        Inherits Being
    
        Sub New()
            Console.WriteLine("Human is created")
        End Sub
    
    End Class
    
    Sub Main()
        
        Dim h As New Human
        
    End Sub
    
End Module
</pre>

<p>
In this program, we have two classes. A base Being class and a derived  Human class. 
The derived class inherits from the base class. 
</p>

<pre class="explanation">
Class Human 
    Inherits Being
</pre>

<p> 
In Visual Basic, we use the <code>Inherits</code> keyword to create  
inheritance relations.
</p>

<pre class="explanation">
Dim h As New Human
</pre>

<p>
We instantiate the derived Human class. 
</p>

<pre>
$ ./inheritance.exe 
Being is created
Human is created
</pre>

<p>
We can see, that both constructors were called. First, the constructor of the
base class is called, then the constructor of the derived class.
</p>

<hr class="btm">

<p>
A more complex example follows. 
</p>

<pre class="code">
Option Strict On


Module Example

    Class Being
    
        Dim Shared Count As Integer = 0
    
        Sub New()
            Count = Count + 1
            Console.WriteLine("Being is created")
        End Sub
        
        Sub GetCount()
            Console.WriteLine("There are {0} Beings", Count)
        End Sub
        
    End Class
    
    Class Human 
        Inherits Being
    
        Sub New()
            Console.WriteLine("Human is created")
        End Sub
    
    End Class
    
    Class Animal 
        Inherits Being
        
        Sub New
            Console.WriteLine("Animal is created")
        End Sub
        
    End Class
    
    Class Dog
        Inherits Animal
        
        Sub New()
            Console.WriteLine("Dog is created")
        End Sub
        
    End Class
    

    Sub Main()
        
        Dim h As New Human
        Dim d As New Dog
        d.GetCount()
        
    End Sub
    
End Module
</pre>

<p>
We have four classes. The inheritance hierarchy is more complicated. The
Human and the Animal classes inherit from the Being class. And the Dog class inherits directly
from the Animal class and indirectly from the Being class. We also introduce a 
concept of a <code>Shared</code> variable.
</p>

<pre class="explanation">
Dim Shared Count As Integer = 0
</pre>

<p>
We define a <code>Shared</code> variable. Shared members
are members, that are shared by all instances of a class. In other
programming languages, they are called static members.
</p>

<pre class="explanation">
Sub New()
    Count = Count + 1
    Console.WriteLine("Being is created")
End Sub
</pre>

<p>
Each time the Being class is instantiated, we increase the Count
variable by one. This way we keep track of the number of instances created.
</p>

<pre class="explanation">
Class Animal 
    Inherits Being
...
Class Dog
    Inherits Animal
...
</pre>

<p>
The Animal inherits from the Being and the Dog inherits from the Animal. Indirectly,
the Dog inherits from the Being as well. 
</p>

<pre class="explanation">
Dim h As New Human
Dim d As New Dog
d.GetCount
</pre>

<p>
We create instances from the Human and from the Dog classes.
We call the GetCount() method of the Dog object. 
</p>

<pre>
$ ./inheritance2.exe 
Being is created
Human is created
Being is created
Animal is created
Dog is created
There are 2 Beings
</pre>

<p>
The Human object calls two constructors. The Dog object calls
three constructors. There are two Beings instantiated.
</p>

<h2>Abstract classes and methods</h2>

<p>
Abstract classes cannot be instantiated. If a class contains at least one abstract method, 
it must be declared abstract too. Abstract methods cannot be implemented,
they merely declare the methods' signatures. When we inherit from an abstract class, all abstract methods
must be implemented by the derived class. Furthermore, these methods must be declared with the same
of less restricted visibility. 
</p>

<p>
Unlike <b>Interfaces</b>, abstract classes may have methods with full implementation and may also have
defined member fields. So abstract classes may provide a partial implementation. Programmers often put
some common functionality into abstract classes. And these abstract classes are later subclassed to 
provide more specific implementation. For example, the Qt graphics library has a QAbstractButton, which 
is the abstract base class of button widgets, providing functionality common to buttons. Buttons 
Q3Button, QCheckBox, QPushButton, QRadioButton, and QToolButton inherit from this base abstract class. 
</p>

<p>
Formally put, abstract classes are used to enforce a protocol. A protocol is a set of operations, 
which all implementing objects must support. 
</p>

<pre class="code">
Option Strict On


Module Example

    MustInherit Class Drawing
        Protected x As Integer = 0
        Protected y As Integer = 0

        Public MustOverride Function Area() As Double

        Public Function GetCoordinates() As String
            Return String.Format("x: {0}, y: {1}", _
                Me.x, Me.y)
        End Function
    
    End Class

    Class Circle  
        Inherits Drawing

        Private Radius As Integer

        Sub New(ByVal x As Integer, ByVal y As Integer, _
            ByVal r As Integer)
            Me.x = x
            Me.y = y
            Me.Radius = r
        End Sub 

        Public Overrides Function Area() As Double
            Return Me.Radius * Me.Radius * Math.PI
        End Function

        Public Overrides Function ToString() As String
            Return String.Format("Circle, at x: {0}, y: {1}, radius: {2}", _
                Me.x, Me.y, Me.Radius)
        End Function

    End Class

    Sub Main()
        
        Dim c as New Circle(12, 45, 22)
       
        Console.WriteLine(c)
        Console.WriteLine("Area of circle: {0}", c.Area())
        Console.WriteLine(c.GetCoordinates())

    End Sub
    
End Module

</pre>

<p>
We have an abstract base Drawing class. 
The class defines two member fields, defines one method and declares one method.
One of the methods is abstract, the other one is fully implemented. The Drawing class
is abstract, because we cannot draw it. We can draw a circle, a dot or a square. 
The Drawing class has some common functionality to the objects, that
we can draw. 
</p>

<pre class="explanation">
MustInherit Class Drawing
</pre>

<p>
In Visual Basic, we use the <code>MustInherit</code> keyword to 
define an abstract class. 
</p>

<pre class="explanation">
Public MustOverride Function Area() As Double
</pre>

<p>
An abstract method is preceded with a <code>MustOverride</code> keyword.
</p>

<pre class="explanation">
Class Circle  
    Inherits Drawing
</pre>

<p>
A Circle is a subclass of the Drawing class. It must implement the
abstract Area() method. 
</p>

<pre>
$ ./abstractclass.exe 
Circle, at x: 12, y: 45, radius: 22
Area of circle: 1520.53084433746
x: 12, y: 45
</pre>

<p>
Output of the program. 
</p>

<p>
This was the first part of the description of OOP in Visual Basic.
</p>


<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div>
<br>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified September 9, 2010  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

